// 获取本地存储的好友列表
const friends = uni.getStorageSync("addressBooks");

// 添加好友函数
export function addFriend(user, customize_name) {
    try {
        if (!isFriend(user)) {
            const newFriend = {
                userSet: {
                    isStick: false,
                    customize_name: customize_name, //备注名
                    customize_pic: '' //备注头像
                },
                userInfo: {
                    userId: user.sendUserId,
                    nicName: user.nicName,
                    nicPic: user.nicPic,
                    userTel: user.userTel,
                    signature: ''
                }
            };
            friends.push(newFriend);

            uni.setStorage({
                key: "addressBooks",
                data: friends,
                success() {
                    uni.showToast({
                        icon:'success',
                        title: '添加电话本成功'
                    });
                    uni.navigateTo({
                        url: '/pages/contacts/contacts'
                    });
                }
            });
        }
    } catch (e) {
        // 异常处理
    }
}

// 判断是否为好友函数
export function isFriend(user) {
    let friends = uni.getStorageSync("addressBooks");
    // 如果本地存储中没有好友列表，则初始化为空数组
    if (!friends) {
        friends = [];
        return false;
    }
    let isExisting = false;
    for (let i = 0; i < friends.length; i++) {
        if (friends[i].userInfo.userTel === user.userTel) {
            // 如果存在，则更新该好友的信息（这里代码有误，newFriend未定义，可根据需求修改）
            // friends[i] = newFriend;
            isExisting = true;
            break;
        }
    }
    return isExisting;
}

// 删除好友函数
export function deleteFriend(userTel) {
    let friends = uni.getStorageSync("addressBooks");
    if (!friends) {
        friends = [];
        return;
    }
    const updatedFriends = friends.filter(friend => friend.userInfo.userTel!== userTel);
    uni.setStorage({
        key: "addressBooks",
        data: updatedFriends,
        success() {
            uni.showToast({
                icon:'success',
                title: '删除好友成功'
            });
        },
        fail() {
            uni.showToast({
                icon: 'none',
                title: '删除好友失败'
            });
        }
    });
}
export function modifyFriend(user, newCustomizeName) {
    let friends = uni.getStorageSync("addressBooks");
    if (!friends) {
        friends = [];
        return;
    }
    const updatedFriends = friends.map(friend => {
        if (friend.userInfo.userTel === user.userTel) {
            return {
               ...friend,
                userSet: {
                   ...friend.userSet,
                    customize_name: newCustomizeName
                }
            };
        }
        return friend;
    });
    uni.setStorage({
        key: 'addbooks',
        data: updatedFriends,
        success() {
            uni.showToast({
                icon:'success',
                title: '修改好友信息成功'
            });
        },
        fail() {
            uni.showToast({
                icon: 'none',
                title: '修改好友信息失败'
            });
        }
    });
}
export function getFriends(user) {
    for (const friend of friends) {
        if (friend.userInfo.userId === user.sendUserId) {
            return friend;
        }
    }
    // 如果没有找到匹配的好友，返回 null 或者其他表示未找到的值
    return null;
}